package com.hskn.hss.module.carTrackOriginal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.module.carTrackOriginal.entity.CarTrackOriginal;
import com.hskn.hss.module.carTrackOriginal.mapper.CarTrackOriginalMapper;
import com.hskn.hss.module.carTrackOriginal.service.CarTrackOriginalService;
import com.hskn.hss.module.common.to.OilNumTO;
import com.hskn.hss.module.kfkcarlocation.entity.KfkCarLocation;
import com.hskn.hss.module.kfkcarlocation.service.IKfkCarLocationService;
import com.hskn.hss.module.tlcar.entity.TlCar;
import com.hskn.hss.module.tlcar.service.ITlCarService;
import com.hskn.hss.utils.LocationUtils;
import com.hskn.hss.utils.StringUtils;
import com.hskn.hss.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-21
 */
@Slf4j
@Service
public class CarTrackOriginalServiceImpl extends ServiceImpl<CarTrackOriginalMapper, CarTrackOriginal> implements CarTrackOriginalService {
    @Autowired
    LocationUtils locationUtil;
    @Resource
    private CarTrackOriginalMapper carTrackOriginalMapper;
    @Resource
    private ITlCarService carService;
    @Resource
    private IKfkCarLocationService kfkCarLocationService;

    @Override
    public void processingStopPointCarTrack(OilNumTO oilNumTO) {
        List<Date> dateList = Tools.getBetweenDateList(oilNumTO.getStartTime(), oilNumTO.getEndTime());
        SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd");
        List<TlCar> carList = carService.list();
        List<CarTrackOriginal> deleteList = new ArrayList<>();
        for (Date date : dateList) {
            for (TlCar car : carList) {
                List<CarTrackOriginal> carTrackOriginalList = carTrackOriginalMapper.selectList(new QueryWrapper<CarTrackOriginal>().lambda()
                        .between(CarTrackOriginal::getTimestamp, Tools.getDateForBegin(date),
                                Tools.getDateForEnd(date))
                        .eq(CarTrackOriginal::getCarId, car.getId()).orderByAsc(CarTrackOriginal::getTimestamp));
                deleteList.addAll(processingStopPointCarTrack(carTrackOriginalList));
            }
            log.info(sp.format(date) + "--processingStopPointCarTrackprocessingStopPointCarTrackprocessingStopPointCarTrack");
        }
        List<String> ids = deleteList.stream().map(CarTrackOriginal::getId).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(ids)) {
            carTrackOriginalMapper.deleteBatchIds(ids);
        }

    }

    private List<CarTrackOriginal> processingStopPointCarTrack(List<CarTrackOriginal> carTrackOriginalList) {
        List<CarTrackOriginal> result = new ArrayList<>();
        for (int i = 1; i < carTrackOriginalList.size(); i++) {
            CarTrackOriginal thisTrack = carTrackOriginalList.get(i);
            CarTrackOriginal lastTrack = carTrackOriginalList.get(i - 1);
            Long thisTrackTimeNum = thisTrack.getTimestamp().getTime();
            Long lastTrackTimeNum = lastTrack.getTimestamp().getTime();
            Long TrackTimeNumDad = thisTrackTimeNum - lastTrackTimeNum;
            double differenceMeter = locationUtil.getDistance(lastTrack.getLat(), lastTrack.getLng(), thisTrack.getLat(), thisTrack.getLng());
            if (locationUtil.realOffLine(differenceMeter, TrackTimeNumDad)) {
                result.add(thisTrack);
            }

        }
        return result;
    }

    @Override
    public void supplementaryCarTrackOriginalPoints(OilNumTO oilNumTO) {
        List<CarTrackOriginal> insertList = new ArrayList<>();
        List<KfkCarLocation> kfkCarLocationList = kfkCarLocationService.list(new QueryWrapper<KfkCarLocation>().lambda()
                .between(KfkCarLocation::getTimestamp, oilNumTO.getStartTime(), oilNumTO.getEndTime())
                .orderByAsc(KfkCarLocation::getTimestamp));
        insertList.addAll(supplementaryCarTrackOriginalPoints(kfkCarLocationList));
        if (CollectionUtils.isNotEmpty(insertList)) {
            saveBatch(insertList);
        }

    }

    private List<CarTrackOriginal> supplementaryCarTrackOriginalPoints(List<KfkCarLocation> kfkCarLocationList) {
        List<CarTrackOriginal> result = new ArrayList<>();
        for (KfkCarLocation kfkCarLocation : kfkCarLocationList) {
            result.add(buildCarTrackOriginal(kfkCarLocation));
        }
        return result;
    }


    private CarTrackOriginal buildCarTrackOriginal(KfkCarLocation kfkCarLocation) {
        CarTrackOriginal currentCarTrackOriginal = new CarTrackOriginal();
        currentCarTrackOriginal.setId(UUID.randomUUID().toString());
        currentCarTrackOriginal.setCarId(kfkCarLocation.getCarid());
        currentCarTrackOriginal.setEqpNum(kfkCarLocation.getEqpnum());
        currentCarTrackOriginal.setWorkMonth(StringUtils.dateToStr(kfkCarLocation.getTimestamp(), "yyyy-MM"));
        currentCarTrackOriginal.setWorkDay(kfkCarLocation.getTimestamp());
        currentCarTrackOriginal.setLng(kfkCarLocation.getLon());
        currentCarTrackOriginal.setLat(kfkCarLocation.getLat());
        currentCarTrackOriginal.setLocation(kfkCarLocation.getLon() + "," + kfkCarLocation.getLat());
        currentCarTrackOriginal.setAddress(locationUtil.getAddress(kfkCarLocation.getLon(), kfkCarLocation.getLat()));
        currentCarTrackOriginal.setSpeed(kfkCarLocation.getSpeed());
        currentCarTrackOriginal.setMessage("");
        currentCarTrackOriginal.setIsDanger(0);
        currentCarTrackOriginal.setIsWarning(0);
        currentCarTrackOriginal.setTimestamp(kfkCarLocation.getTimestamp());
        currentCarTrackOriginal.setStatus(Integer.parseInt(kfkCarLocation.getStatus()));
        currentCarTrackOriginal.setCreateTime(new Date());
        currentCarTrackOriginal.setDangerOil(0.0);
        currentCarTrackOriginal.setDangerOverSpeed(0);
        currentCarTrackOriginal.setWarningOverSpeed(0);
        currentCarTrackOriginal.setWarningDriveEmpty(0);
        currentCarTrackOriginal.setWarningLowSpeed(0);
        currentCarTrackOriginal.setWarningStay(0);
        currentCarTrackOriginal.setWarningLeave(0);
        currentCarTrackOriginal.setWarningOil(0);
        currentCarTrackOriginal.setMoveSecond(0);
        currentCarTrackOriginal.setMileage(kfkCarLocation.getMileage());
        currentCarTrackOriginal.setIsInGrid(0);
        currentCarTrackOriginal.setDirection(kfkCarLocation.getDirection());
        return currentCarTrackOriginal;
    }
}
